﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using XiAnAirport.DAL;
using XiAnAirport.ElevatorWebSocket;
using XiAnAirport.Models;

namespace XiAnAirport.BUS
{
    public class ElevatorAreaBUS
    {
        public ResponseMessage AddElevator(Elevator elevator)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                string result = da.AddElevator(elevator);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                    UpdateElevator();
                    //InitDevice();
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "新增设备失败";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = "新增设备异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增电梯";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage EditElevator(Elevator elevatorinfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            string result = da.EditElevator(elevatorinfo);

            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
                UpdateElevator(elevatorinfo);
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "修改设备信息失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage DelElevator(int elevatorid)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            string result = string.Empty;
            try
            {
                result = da.DelElevator(elevatorid);
                if (result == "success")
                {
                    result = da.DelElevatorPosition(elevatorid);
                    UpdateElevator();
                }
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除电梯";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage BatchDelElevator(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            string result = string.Empty;
            try
            {
                result = da.BatchDelElevator(ids);
                if (result == "success")
                {
                    result = da.BatchDelElevatorPosition(ids);
                    UpdateElevator();
                }
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除电梯";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage QueryElevator(Elevator elevatorinfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                List<Elevator> content = da.QueryElevator(elevatorinfo);

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = da.QueryElevatorConut(elevatorinfo);
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }

            return message;
        }

        public ResponseMessage QueryElevatorByArea(Elevator elevatorinfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                ElevatorStateBUS mBUS = new ElevatorStateBUS();
                List<ElevatorState> list = mBUS.GetData();
                List<Elevator> content = da.QueryElevatorByArea(elevatorinfo);
                for (int i = 0; i < content.Count; i++)
                {
                    Elevator elevator = content[i];
                    ElevatorState elevatorState = list.FirstOrDefault(x => x.opcNO == elevator.opcNO);
                    elevator.Elevator_control_status = elevatorState.Elevator_control_status;
                    elevator.Elevator_control_statusName = elevatorState.Elevator_control_statusName;
                    elevator.Elevator_control_statusNameEn = elevatorState.Elevator_control_statusNameEn;
                    elevator.Elevator_moving_status = elevatorState.Elevator_moving_status;
                    content[i] = elevator;
                }

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = da.QueryElevatorByAreaConut(elevatorinfo);
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }

            return message;
        }

        public ResponseMessage LoadElevator(int elevatorid)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                Elevator content = da.LoadElevator(elevatorid);
                if (content != null)
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = content;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "未查询到相关信息";
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入电梯";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage CleanUpElevator()
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            string result = string.Empty;
            try
            {
                result = da.CleanUpElevator();
                if (result == "success")
                {
                    UpdateElevator();

                    result = da.CleanUpElevatorPosition();
                }
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "清理所有电梯";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage AddArea(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                string result = da.AddArea(areainfo);
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增区域";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage UpdateArea(AreaInfo areainfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                //创建xml文档
                XmlDocument xmldoc = new XmlDocument();

                //导入指定xml文件
                xmldoc.Load("AreaInfo.xml");

                //获取根节点
                XmlElement employees = xmldoc.DocumentElement;

                //修改节点
                string nodename = "//Area" + areainfo.id;
                XmlElement nodeUpdate = employees.SelectSingleNode(nodename) as XmlElement;//筛选匹配的第一个节点
                nodeUpdate["AreaName"].InnerText = areainfo.areaName;

                //保存创建好的XML文档
                xmldoc.Save("AreaInfo.xml");
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "";
                LogHelper.Log(ex.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改区域";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage DelArea(int areaid)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            string result = string.Empty;
            try
            {
                result = da.DelArea(areaid);
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除区域";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage BatchDelArea(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            string result = string.Empty;
            try
            {


                result = da.BatchDelArea(ids);
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除区域";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public List<AreaInfo> GetAllArea()
        {
            List<AreaInfo> areaInfos = new List<AreaInfo>();
            //创建xml文档
            XmlDocument xmldoc = new XmlDocument();

            //导入指定xml文件
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释，注释也是节点，但是无法转换成XmlElement，所以这里不忽略，下面转换就报错
                                           //XmlReader是用liu需要using包起来，读完释放资源，防止占用
            using (XmlReader reader = XmlReader.Create("AreaInfo.xml", settings))
            {
                xmldoc.Load(reader);
            }

            //获取根节点
            XmlElement areaxml = xmldoc.DocumentElement;

            //遍历根节点的子节点
            foreach (XmlElement area in areaxml.ChildNodes)
            {
                AreaInfo areaInfo = new AreaInfo();

                foreach (XmlElement child in area.ChildNodes)
                {
                    if (child.Name == "AreaId")
                    {
                        areaInfo.id = Convert.ToInt32(child.InnerText);
                    }
                    else if (child.Name == "AreaName")
                    {
                        areaInfo.areaName = child.InnerText;
                    }
                    else if (child.Name == "ParentAreaId")
                    {
                        areaInfo.mainAreaId = Convert.ToInt32(child.InnerText);
                    }
                }
                areaInfos.Add(areaInfo);
            }
            return areaInfos;
        }

        public ResponseMessage QueryArea()
        {
            List<AreaInfo> list = new List<AreaInfo>();
            List<AreaInfo> areaInfos = new List<AreaInfo>();
            ResponseMessage message = new ResponseMessage();
            areaInfos = GetAllArea();
            List<AreaInfo> elist = areaInfos.FindAll(delegate (AreaInfo e) { return e.mainAreaId == 0; });
            foreach (var item in elist)
            {
                AreaInfo areaInfo = new AreaInfo();
                areaInfo.id = item.id;
                areaInfo.areaName = item.areaName;
                areaInfo.mainAreaId = 0;
                areaInfo.child = areaInfos.FindAll(delegate (AreaInfo e) { return e.mainAreaId == item.id; });
                list.Add(areaInfo);
            }
            message.data = list;
            return message;
        }

        public ResponseMessage AddElevatorPosition(ElevatorPosition positioninfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                string result = da.AddElevatorPosition(positioninfo);
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增设备区域绑定";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage EditElevatorPosition(ElevatorPosition positioninfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                string result = da.EditElevatorPosition(positioninfo);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改设备区域绑定";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage DelElevatorPosition(int id)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                string result = da.DelPosition(id);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除设备区域绑定";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage QueryElevatorState(ElevatorState stateinfo, List<ElevatorState> list)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                if (stateinfo.AreaID > 0)
                {
                    list = list.FindAll(delegate (ElevatorState e) { return e.AreaID == stateinfo.AreaID; });
                }
                if (stateinfo.Elevator_moving_status > 0)
                {
                    list = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == stateinfo.Elevator_moving_status; });
                }
                if (!string.IsNullOrEmpty(stateinfo.ElevatorName))
                {
                    list = list.FindAll(delegate (ElevatorState e) { return e.ElevatorName.ToLower().Contains(stateinfo.ElevatorName.ToLower()); });
                }
                if (!string.IsNullOrEmpty(stateinfo.ElevatorNO))
                {
                    list = list.FindAll(delegate (ElevatorState e) { return e.ElevatorNO.ToLower().Contains(stateinfo.ElevatorNO.ToLower()); });
                }

                int count = list.Count;
                if (stateinfo.Recordhead > -1 && stateinfo.Recordtail > 0)
                {
                    if (count < stateinfo.Recordtail)
                    {
                        stateinfo.Recordtail = count;
                    }
                    if (stateinfo.Recordhead + stateinfo.Recordtail < count)
                    {
                        list = list.GetRange(stateinfo.Recordhead, stateinfo.Recordtail);
                    }
                    else
                    {
                        list = list.GetRange(stateinfo.Recordhead, count - stateinfo.Recordhead);
                    }

                }

                message.errcode = 0;
                message.errmsg = "";
                message.data = list;
                message.total = count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询电梯状态异常";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯状态";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage Clearbend(int elevatorid)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                string result = da.Clearbend(elevatorid);

                if (result.Contains("success"))
                {
                    bool result_7 = PushPromptMessage("RefreshPrompt").Result;
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "清除折弯次数";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public ResponseMessage QueryElevatorType()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                ElevatorAreaDAL da = new ElevatorAreaDAL();
                List<ElevatorType> list = da.QueryElevatorType();
                message.data = list;
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.ToString());
                message.errcode = 1;
                message.errmsg = "查询电梯类型异常";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯类型";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public static async Task<bool> PushPromptMessage(string message)
        {
            return await ElevatorWebSocketMiddleware.webSocketManage.PushPromptMessage(message);
        }

        public void UpdateElevator()
        {
            try
            {
                ElevatorAreaDAL da = new ElevatorAreaDAL();
                Elevator elevator = new Elevator();
                List<Elevator> elevators = da.QueryElevator(elevator);
                List<ElevatorRunInfo> runs_list = da.QueryElevatorRunsTotal();
                List<ElevatorRunInfo> time_list = da.QueryElevatorRunTimeTotal();
                if (elevators != null && elevators.Count > 0)
                {
                    for (int i = 0; i < elevators.Count; i++)
                    {
                        Elevator elevator1 = elevators[i];
                        int run_id = runs_list.FindIndex(x => x.elevatorId == elevator1.id);
                        if (run_id > -1)
                        {
                            elevator1.Runs = runs_list[run_id].runsTotal;
                        }
                        int time_id = time_list.FindIndex(x => x.elevatorId == elevator1.id);
                        if (time_id > -1)
                        {
                            elevator1.RunTimeSecond = time_list[time_id].runTimeTotal;
                        }
                        elevators[i] = elevator1;
                    }
                }
                ElevatorStateBUS.device_list = elevators;
                int devicetotal = 0;
                if (ElevatorStateBUS.device_list != null)
                {
                    devicetotal = ElevatorStateBUS.device_list.Count;
                }
                ElevatorStateBUS bus = new ElevatorStateBUS();
                List<ElevatorState> list = bus.GetData();
                int maintenanceTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 4; }).Count;
                int faultTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 3; }).Count;
                int normalTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 2; }).Count;
                int offTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 1; }).Count;
                if (ElevatorStateBUS.KoneDeviceStatus != null)
                {
                    KoneDeviceStatus koneDevice = new KoneDeviceStatus();
                    koneDevice.offlineTotal = offTotal;
                    koneDevice.normalTotal = normalTotal;
                    koneDevice.faultTotal = faultTotal;
                    koneDevice.maintenanceTotal = maintenanceTotal;
                    koneDevice.deviceTotal = devicetotal;
                    ElevatorStateBUS.KoneDeviceStatus = koneDevice;
                }
            }
            catch
            {

            }
        }

        public void UpdateElevator(Elevator elevatorinfo)
        {
            try
            {
                ElevatorAreaDAL da = new ElevatorAreaDAL();
                Elevator elevator = new Elevator();
                List<Elevator> elevators = da.QueryElevator(elevator);
                List<ElevatorRunInfo> runs_list = da.QueryElevatorRunsTotal();
                List<ElevatorRunInfo> time_list = da.QueryElevatorRunTimeTotal();
                if (elevators != null && elevators.Count > 0)
                {
                    for (int i = 0; i < elevators.Count; i++)
                    {
                        Elevator elevator1 = elevators[i];
                        int run_id = runs_list.FindIndex(x => x.elevatorId == elevator1.id);
                        if (run_id > -1)
                        {
                            elevator1.Runs = runs_list[run_id].runsTotal;
                        }
                        int time_id = time_list.FindIndex(x => x.elevatorId == elevator1.id);
                        if (time_id > -1)
                        {
                            elevator1.RunTimeSecond = time_list[time_id].runTimeTotal;
                        }
                        elevators[i] = elevator1;
                    }
                }
                ElevatorStateBUS.device_list = elevators;
                int devicetotal = 0;
                if (ElevatorStateBUS.device_list != null)
                {
                    devicetotal = ElevatorStateBUS.device_list.Count;
                }
                ElevatorStateBUS bus = new ElevatorStateBUS();
                bus.UpdateFloor(elevatorinfo);
                List<ElevatorState> list = bus.GetData();
                int maintenanceTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 4; }).Count;
                int faultTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 3; }).Count;
                int normalTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 2; }).Count;
                int offTotal = list.FindAll(delegate (ElevatorState e) { return e.Elevator_moving_status == 1; }).Count;
                if (ElevatorStateBUS.KoneDeviceStatus != null)
                {
                    KoneDeviceStatus koneDevice = new KoneDeviceStatus();
                    koneDevice.offlineTotal = offTotal;
                    koneDevice.normalTotal = normalTotal;
                    koneDevice.faultTotal = faultTotal;
                    koneDevice.maintenanceTotal = maintenanceTotal;
                    koneDevice.deviceTotal = devicetotal;
                    ElevatorStateBUS.KoneDeviceStatus = koneDevice;
                }
            }
            catch
            {

            }
        }

        public ResponseMessage ClearRunsSum(int elevatorid)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                string result = da.ClearRunSum(elevatorid);

                if (result.Contains("success"))
                {
                    bool result_7 = PushPromptMessage("RefreshPrompt").Result;
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "清除折弯次数";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        public List<PanoramicElevatorData> QueryPanoramicElevator(int Recordhead, int Recordtail)
        {
            List<PanoramicElevatorData> panoramics = new List<PanoramicElevatorData>();
            List<PanoramicElevator> content = new List<PanoramicElevator>();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                content = da.QueryPanoramicElevator(Recordhead, Recordtail);
                List<LiftStateALL> list = ElevatorStateBUS.state_list;
                foreach (var item in content)
                {
                    PanoramicElevatorData panoramicElevatorData = new PanoramicElevatorData();
                    int index = ElevatorStateBUS.state_list.FindIndex(x => x.opcNO == item.opcNO);
                    if (index > -1)
                    {
                        LiftStateALL liftStateALL = ElevatorStateBUS.state_list[index];
                        int state = liftStateALL.Elevator_moving_status;
                        panoramicElevatorData.status = state == 1 ? 0 : 1;
                        panoramicElevatorData.direction = liftStateALL.Moving_direction;
                        panoramicElevatorData.fault = liftStateALL.Elevator_moving_status == 3 ? 1 : 0;
                        panoramicElevatorData.floor = liftStateALL.Car_position;
                        if (liftStateALL.Car_door.Length == 1)
                        {
                            panoramicElevatorData.frontdoorStatus = Convert.ToInt32(liftStateALL.Car_door.Substring(0, 1));
                            panoramicElevatorData.backdoorStatus = 2;
                        }
                        else if (liftStateALL.Car_door.Length == 2)
                        {
                            panoramicElevatorData.frontdoorStatus = Convert.ToInt32(liftStateALL.Car_door.Substring(0, 1));
                            panoramicElevatorData.backdoorStatus = Convert.ToInt32(liftStateALL.Car_door.Substring(1, 1));
                        }
                        else
                        {
                            panoramicElevatorData.frontdoorStatus = 2;
                            panoramicElevatorData.backdoorStatus = 2;
                        }
                    }
                    else
                    {
                        panoramicElevatorData.status = 0;
                        panoramicElevatorData.direction =2 ;
                        panoramicElevatorData.fault = 0;
                        panoramicElevatorData.floor = "";
                        panoramicElevatorData.frontdoorStatus = 2;
                        panoramicElevatorData.backdoorStatus = 2;
                    }
                    panoramicElevatorData.assetCode = item.elevatorNo;
                    panoramicElevatorData.assetManufacturer = "KONE";
                    panoramicElevatorData.assetName = item.elevatorName;
                    panoramicElevatorData.assetLocation = item.mainAreaName + "-" + item.areaName;
                    panoramicElevatorData.systemCode = "EAEMS";
                    panoramicElevatorData.assetModelName = item.model;
                    panoramicElevatorData.assetTypeCode = item.elevatorType;
                    panoramics.Add(panoramicElevatorData);
                }

            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }

            return panoramics;
        }

        public List<PanoramicElevatorData> QueryPanoramicElevator(string code)
        {
            List<PanoramicElevatorData> panoramics = new List<PanoramicElevatorData>();
            List<PanoramicElevator> content = new List<PanoramicElevator>();
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                content = da.QueryPanoramicElevator(code);
                List<LiftStateALL> list = ElevatorStateBUS.state_list;
                foreach (var item in content)
                {
                    PanoramicElevatorData panoramicElevatorData = new PanoramicElevatorData();
                    int index = ElevatorStateBUS.state_list.FindIndex(x => x.opcNO == item.opcNO);
                    if (index > -1)
                    {
                        LiftStateALL liftStateALL = ElevatorStateBUS.state_list[index];
                        int state = liftStateALL.Elevator_moving_status;
                        panoramicElevatorData.status = state == 1 ? 0 : 1;
                        panoramicElevatorData.direction = liftStateALL.Moving_direction;
                        panoramicElevatorData.fault = liftStateALL.Elevator_moving_status == 3 ? 1 : 0;
                        panoramicElevatorData.floor = liftStateALL.Car_position;
                        if (liftStateALL.Car_door.Length == 1)
                        {
                            panoramicElevatorData.frontdoorStatus = Convert.ToInt32(liftStateALL.Car_door.Substring(0, 1));
                            panoramicElevatorData.backdoorStatus = 2;
                        }
                        else if (liftStateALL.Car_door.Length == 2)
                        {
                            panoramicElevatorData.frontdoorStatus = Convert.ToInt32(liftStateALL.Car_door.Substring(0, 1));
                            panoramicElevatorData.backdoorStatus = Convert.ToInt32(liftStateALL.Car_door.Substring(1, 1));
                        }
                        else
                        {
                            panoramicElevatorData.frontdoorStatus = 2;
                            panoramicElevatorData.backdoorStatus = 2;
                        }
                    }
                    else
                    {
                        panoramicElevatorData.status = 0;
                        panoramicElevatorData.direction = 2;
                        panoramicElevatorData.fault = 0;
                        panoramicElevatorData.floor = "";
                        panoramicElevatorData.frontdoorStatus = 2;
                        panoramicElevatorData.backdoorStatus = 2;
                    }
                    panoramicElevatorData.assetCode = item.elevatorNo;
                    panoramicElevatorData.assetManufacturer = "KONE";
                    panoramicElevatorData.assetName = item.elevatorName;
                    panoramicElevatorData.assetLocation = item.mainAreaName + "-" + item.areaName;
                    panoramicElevatorData.systemCode = "EAEMS";
                    panoramicElevatorData.assetModelName = item.model;
                    panoramicElevatorData.assetTypeCode = item.elevatorType;
                    panoramics.Add(panoramicElevatorData);
                }

            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }

            return panoramics;
        }
    }
}
